/*
 * @Author: Yuluo 568055454@qq.com
 * @Date: 2023年06月16日 22:59:01
 * @LastEditors: Yuluo 568055454@qq.com
 * @LastEditTime: 2023年07月16日 03:57:32
 * @FilePath: \Mood-Blog-System\back\models\User.js
 * @Description: User表模型定义文件
 * Copyright (c) 2023 by Yuluo, All Rights Reserved. 
 */
const config = require("../config");
const { mongoose } = require('../db')

const schema = new mongoose.Schema({
    username: {
        type: String,
        required: true,
        unique: true,
        max: 10,
        trim: true,
        index: true,
    },
    password: {
        type: String,
        required: true,
        max: 20,
        trim: true,
    },
    phone: { type: String, unique: true, match: /^1[3456789]\d{9}$/ },
    email: { type: String, unique: true, match: /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ },
    avatar: { type: String, default: config.defaultAvatar },
    cover: { type: String, default: config.defaultCover },
    role: { type: String, default: 'user', enum: ['visitor', 'user', 'admin'] },
    status: { type: String, default: 'normal', enum: ['normal', 'disabled', 'deleted'] },
    token: { type: String, default: '' },
    bio: { type: String, default: '' },
    lastLoginAt: { type: Date, default: Date.now },
    deletedAt: { type: Date, default: null },
}, {
    timestamps: true,
});

schema.plugin(require('mongoose-autopopulate'));
schema.plugin(require('mongoose-unique-validator'));
module.exports = mongoose.model('User', schema, 'user');